﻿namespace Blaze.Editor.BehaviourTree
{
    using System;
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// 行为树设计窗口。
    /// </summary>
    public class DesignerWindow : EditorWindow
    {
        public DesignerWindow()
        {
            mGraphScrollSize = new Vector2(20000f, 20000f);
//            mPropertiesPanelOnLeft = true;
//            mCurrentMousePosition = Vector2.zero;
//            mGraphStatus = string.Empty;
//            mActiveBehaviorID = -1;
//            mBehaviorSourceHistory = new List<Object>();
//            mBehaviorSourceHistoryIndex = -1;
            mLastUpdateCheck = DateTime.MinValue;
        }

        [MenuItem("Blaze/面板/行为树设计", false, BlazeEditorMenu.Priority.Panels)]
        public static void ShowWindow()
        {
            var window = GetWindow<DesignerWindow>(false, "行为树设计");
            window.wantsMouseMove = true;
            window.minSize = new Vector2(500f, 100f);
            window.init();
        }

        public void OnGUI()
        {
            mCurrentMousePosition = Event.current.mousePosition;
            setupSizes();
            if (!mSizesInitialized)
            {
                mSizesInitialized = true;
//                if (!mLockActiveGameObject || (mActiveObject == null))
//                {
//                    UpdateTree(true);
//                }
//                else
//                {
//                    ReloadPreviousBehavior();
//                }
            }
            if (draw() && (mGUITickCount > 1))
            {
                Repaint();
                mGUITickCount = 0;
            }
            handleEvents();
            mGUITickCount++;
        }

        public bool ViewOnlyMode()
        {
            if (Application.isPlaying)
            {
                return false;
            }
//            if (((this.mActiveBehaviorSource == null) || (this.mActiveBehaviorSource.get_Owner() == null)) || this.mActiveBehaviorSource.get_Owner().Equals(null))
//            {
//                return false;
//            }
//            Behavior behavior = this.mActiveBehaviorSource.get_Owner().GetObject() as Behavior;
//            return (((behavior != null) && !BehaviorDesignerPreferences.GetBool(BDPreferences.EditablePrefabInstances)) && (PrefabUtility.GetPrefabType(this.mActiveBehaviorSource.get_Owner().GetObject()) == PrefabType.PrefabInstance));
            return false;
        }

        private bool CheckForAutoScroll()
        {
            Vector2 vector;
            if (!GetMousePositionInGraph(out vector))
            {
                return false;
            }
            if (mGraphScrollRect.Contains(mCurrentMousePosition))
            {
                return false;
            }
//            if ((!this.mIsDragging && !this.mIsSelecting) && (this.mGraphDesigner.ActiveNodeConnection == null))
//            {
//                return false;
//            }
            Vector2 zero = Vector2.zero;
            if (mCurrentMousePosition.y < (mGraphScrollRect.yMin + 15f))
            {
                zero.y = 3f;
            }
            else if (mCurrentMousePosition.y > (mGraphScrollRect.yMax - 15f))
            {
                zero.y = -3f;
            }
            if (mCurrentMousePosition.x < (mGraphScrollRect.xMin + 15f))
            {
                zero.x = 3f;
            }
            else if (mCurrentMousePosition.x > (mGraphScrollRect.xMax - 15f))
            {
                zero.x = -3f;
            }
            ScrollGraph(zero);
            if (mIsDragging)
            {
//                this.mGraphDesigner.DragSelectedNodes((Vector2)(-zero / this.mGraphZoom), Event.current.modifiers != EventModifiers.Alt);
            }
            if (mIsSelecting)
            {
                mSelectStartPosition += zero/mGraphZoom;
            }
            return true;
        }

        private bool draw()
        {
            bool flag = false;
            Color color = GUI.color;
            Color backgroundColor = GUI.backgroundColor;
            GUI.color = Color.white;
            GUI.backgroundColor = Color.white;
//            DrawFileToolbar();
//            DrawDebugToolbar();
            DrawPropertiesBox();
            if (drawGraphArea())
            {
                flag = true;
            }
//            DrawPreferencesPane();
//            if (mTakingScreenshot)
//            {
//                GUI.DrawTexture(new Rect(0f, 0f, base.position.width, base.position.height + 22f), BehaviorDesignerUtility.ScreenshotBackgroundTexture, ScaleMode.StretchToFill, false);
//            }
            GUI.color = color;
            GUI.backgroundColor = backgroundColor;
            return flag;
        }

        private bool drawGraphArea()
        {
            Vector2 vector2;
            if ((Event.current.type != EventType.ScrollWheel) && !mTakingScreenshot)
            {
                Vector2 vector = GUI.BeginScrollView(new Rect(mGraphRect.x, mGraphRect.y, mGraphRect.width + 15f, mGraphRect.height + 15f), mGraphScrollPosition, new Rect(0f, 0f, mGraphScrollSize.x, mGraphScrollSize.y), true, true);
                if (((vector != mGraphScrollPosition) && (Event.current.type != EventType.DragUpdated)) && (Event.current.type != EventType.Ignore))
                {
                    mGraphOffset -= (vector - mGraphScrollPosition)/mGraphZoom;
                    mGraphScrollPosition = vector;
                    //mGraphDesigner.GraphDirty();
                }
                GUI.EndScrollView();
            }
            GUI.Box(mGraphRect, string.Empty, DesignerUtility.GraphBackgroundGUIStyle);
            drawGrid();
            EditorZoomArea.Begin(mGraphRect, mGraphZoom);
            if (!GetMousePositionInGraph(out vector2))
            {
                vector2 = new Vector2(-1f, -1f);
            }
            bool flag = false;
//            if ((mGraphDesigner != null) && mGraphDesigner.DrawNodes(vector2, mGraphOffset))
//            {
//                flag = true;
//            }
            if (mTakingScreenshot && (Event.current.type == EventType.Repaint))
            {
                //RenderScreenshotTile();
            }
            if (mIsSelecting)
            {
                GUI.Box(GetSelectionArea(), string.Empty, DesignerUtility.SelectionGUIStyle);
            }
            EditorZoomArea.End();
//            DrawGraphStatus();
//            DrawSelectedTaskDescription();
            return flag;
        }

        private void DrawPropertiesBox()
        {
            GUILayout.BeginArea(mPropertyToolbarRect, EditorStyles.toolbar);
            mBehaviorToolbarSelection = GUILayout.Toolbar(mBehaviorToolbarSelection, mBehaviorToolbarStrings, EditorStyles.toolbarButton);
            GUILayout.EndArea();
            GUILayout.BeginArea(mPropertyBoxRect, DesignerUtility.PropertyBoxGUIStyle);
            if (mBehaviorToolbarSelection == 0)
            {
//                if (this.mActiveBehaviorSource != null)
//                {
//                    GUILayout.Space(3f);
//                    BehaviorSource behaviorSource = (this.mExternalParent == null) ? this.mActiveBehaviorSource : this.mExternalParent;
//                    if (behaviorSource.get_Owner() is Behavior)
//                    {
//                        bool externalModification = false;
//                        bool showOptions = false;
//                        if (BehaviorInspector.DrawInspectorGUI(behaviorSource.get_Owner() as Behavior, new SerializedObject(behaviorSource.get_Owner() as Behavior), false, ref externalModification, ref showOptions, ref showOptions))
//                        {
//                            BehaviorDesignerUtility.SetObjectDirty(behaviorSource.get_Owner().GetObject());
//                            if (externalModification)
//                            {
//                                this.LoadBehavior(behaviorSource, false, false);
//                            }
//                        }
//                    }
//                    else
//                    {
//                        bool showVariables = false;
//                        ExternalBehaviorInspector.DrawInspectorGUI(behaviorSource, false, ref showVariables);
//                    }
//                }
//                else
//                {
//                    GUILayout.Space(5f);
//                    GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(285f) };
//                    GUILayout.Label("No behavior tree selected. Create a new behavior tree or select one from the hierarchy.", BehaviorDesignerUtility.LabelWrapGUIStyle, options);
//                }
            }
            else if (mBehaviorToolbarSelection == 1)
            {
//                this.mTaskList.DrawTaskList(this, !this.ViewOnlyMode());
//                if (mBehaviorToolbarSelection != 1)
//                {
//                    this.mTaskList.FocusSearchField();
//                }
            }
            else if (mBehaviorToolbarSelection == 2)
            {
//                if (this.mActiveBehaviorSource != null)
//                {
//                    BehaviorSource source2 = (this.mExternalParent == null) ? this.mActiveBehaviorSource : this.mExternalParent;
//                    if (this.mVariableInspector.DrawVariables(source2))
//                    {
//                        this.SaveBehavior();
//                    }
//                    if (mBehaviorToolbarSelection != 2)
//                    {
//                        this.mVariableInspector.FocusNameField();
//                    }
//                }
//                else
//                {
//                    GUILayout.Space(5f);
//                    GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.Width(285f) };
//                    GUILayout.Label("No behavior tree selected. Create a new behavior tree or select one from the hierarchy.", BehaviorDesignerUtility.LabelWrapGUIStyle, optionArray2);
//                }
            }
            else if (mBehaviorToolbarSelection == 3)
            {
//                if ((this.mGraphDesigner.SelectedNodes.Count == 1) && !this.mGraphDesigner.SelectedNodes[0].IsEntryDisplay)
//                {
//                    Task task = this.mGraphDesigner.SelectedNodes[0].Task;
//                    if ((this.mNodeDesignerTaskMap != null) && (this.mNodeDesignerTaskMap.Count > 0))
//                    {
//                        NodeDesigner key = this.mGraphDesigner.SelectedNodes[0].Task.get_NodeData().get_NodeDesigner() as NodeDesigner;
//                        if ((key != null) && this.mNodeDesignerTaskMap.ContainsKey(key))
//                        {
//                            task = this.mNodeDesignerTaskMap[key];
//                        }
//                    }
//                    if (this.mTaskInspector.DrawTaskInspector(this.mActiveBehaviorSource, this.mTaskList, task, !this.ViewOnlyMode()) && !Application.isPlaying)
//                    {
//                        this.SaveBehavior();
//                    }
//                }
//                else
//                {
//                    GUILayout.Space(5f);
//                    if (this.mGraphDesigner.SelectedNodes.Count > 1)
//                    {
//                        GUILayoutOption[] optionArray3 = new GUILayoutOption[] { GUILayout.Width(285f) };
//                        GUILayout.Label("Only one task can be selected at a time to\n view its properties.", BehaviorDesignerUtility.LabelWrapGUIStyle, optionArray3);
//                    }
//                    else
//                    {
//                        GUILayoutOption[] optionArray4 = new GUILayoutOption[] { GUILayout.Width(285f) };
//                        GUILayout.Label("Select a task from the tree to\nview its properties.", BehaviorDesignerUtility.LabelWrapGUIStyle, optionArray4);
//                    }
//                }
            }
            GUILayout.EndArea();
        }

        private bool GetMousePositionInGraph(out Vector2 mousePosition)
        {
            mousePosition = mCurrentMousePosition;
            if (!mGraphRect.Contains(mousePosition))
            {
                return false;
            }
//            if (mShowPrefPane && mPreferencesPaneRect.Contains(mousePosition))
//            {
//                return false;
//            }
            mousePosition -= new Vector2(mGraphRect.xMin, mGraphRect.yMin);
            mousePosition = mousePosition/mGraphZoom;
            return true;
        }

        private void init()
        {
//            if (mTaskList == null)
//            {
//                mTaskList = ScriptableObject.CreateInstance<TaskList>();
//            }
//            if (mVariableInspector == null)
//            {
//                mVariableInspector = ScriptableObject.CreateInstance<VariableInspector>();
//            }
//            if (mGraphDesigner == null)
//            {
//                mGraphDesigner = ScriptableObject.CreateInstance<GraphDesigner>();
//            }
//            if (mTaskInspector == null)
//            {
//                mTaskInspector = ScriptableObject.CreateInstance<TaskInspector>();
//            }
            if (mGridMaterial == null)
            {
                mGridMaterial = new Material(Shader.Find("Hidden/Blaze/Grid"))
                {
                    hideFlags = HideFlags.HideAndDontSave,
                    shader = {hideFlags = HideFlags.HideAndDontSave}
                };
            }
//            mTaskList.init();
//            FieldInspector.init();
//            ClearBreadcrumbMenu();
        }

        private bool PropertiesInspectorHasFocus()
        {
            // return (this.mTaskInspector.HasFocus() || this.mVariableInspector.HasFocus());
            return true;
        }

        private void ScrollGraph(Vector2 amount)
        {
            mGraphOffset += amount/mGraphZoom;
            mGraphScrollPosition -= amount;
//            this.mGraphDesigner.GraphDirty();
            Repaint();
        }

        private void setupSizes()
        {
            float width = position.width;
            float num2 = position.height + 22f;
            if (((mPrevScreenWidth != width) || (mPrevScreenHeight != num2)))
            {
                mFileToolBarRect = new Rect(300f, 0f, width - 300f, 18f);
                mPropertyToolbarRect = new Rect(0f, 0f, 300f, 18f);
                mPropertyBoxRect = new Rect(0f, this.mPropertyToolbarRect.height, 300f, (num2 - this.mPropertyToolbarRect.height) - 21f);
                mGraphRect = new Rect(300f, 18f, (width - 300f) - 15f, ((num2 - 36f) - 21f) - 15f);
                mPreferencesPaneRect = new Rect((300f + this.mGraphRect.width) - 290f, (float) (0x12 + (!EditorGUIUtility.isProSkin ? 2 : 1)), 290f, 368f);

                mDebugToolBarRect = new Rect(mGraphRect.x, (num2 - 18f) - 21f, mGraphRect.width + 15f, 18f);
                mGraphScrollRect.Set(mGraphRect.xMin + 15f, mGraphRect.yMin + 15f, mGraphRect.width - 30f, mGraphRect.height - 30f);
                if (mGraphScrollPosition == new Vector2(-1f, -1f))
                {
                    mGraphScrollPosition = ((mGraphScrollSize - new Vector2(mGraphRect.width, mGraphRect.height))/2f) - (2f*new Vector2(15f, 15f));
                }
                mPrevScreenWidth = width;
                mPrevScreenHeight = num2;
            }
        }

        private Vector2 mGraphScrollPosition = new Vector2(-1f, -1f);
        private int mGUITickCount;
        private Vector2 mGraphScrollSize;
        private bool mTakingScreenshot;
        private int mBehaviorToolbarSelection = 1;

        private bool mCommandDown;
        private bool mShowRightClickMenu;
        private GenericMenu mRightClickMenu;
        private Rect mPropertyToolbarRect;
        private Rect mPreferencesPaneRect;
        private Rect mPropertyBoxRect;
        private readonly string[] mBehaviorToolbarStrings = {"Behavior", "Tasks", "Variables", "Inspector"};
        private DateTime mLastUpdateCheck;

        #region Selection Area

        private Rect GetSelectionArea()
        {
            Vector2 vector;
            if (GetMousePositionInGraph(out vector))
            {
                float x = (mSelectStartPosition.x >= vector.x) ? vector.x : mSelectStartPosition.x;
                float num2 = (mSelectStartPosition.x <= vector.x) ? vector.x : mSelectStartPosition.x;
                float y = (mSelectStartPosition.y >= vector.y) ? vector.y : mSelectStartPosition.y;
                float num4 = (mSelectStartPosition.y <= vector.y) ? vector.y : mSelectStartPosition.y;
                mSelectionArea = new Rect(x, y, num2 - x, num4 - y);
            }
            return mSelectionArea;
        }

        private Rect mSelectionArea;
        private Vector2 mSelectStartPosition;

        #endregion

        #region Grid

        private void drawGrid()
        {
            mGridMaterial.SetPass(!EditorGUIUtility.isProSkin ? 1 : 0);
            GL.PushMatrix();
            GL.Begin(1);
            drawGridLines(10f*mGraphZoom, new Vector2((mGraphOffset.x%10f)*mGraphZoom, (mGraphOffset.y%10f)*mGraphZoom));
            GL.End();
            GL.PopMatrix();
            mGridMaterial.SetPass(!EditorGUIUtility.isProSkin ? 3 : 2);
            GL.PushMatrix();
            GL.Begin(1);
            drawGridLines(50f*mGraphZoom, new Vector2((mGraphOffset.x%50f)*mGraphZoom, (mGraphOffset.y%50f)*mGraphZoom));
            GL.End();
            GL.PopMatrix();
        }

        private void drawGridLines(float gridSize, Vector2 offset)
        {
            float num = mGraphRect.x + offset.x;
            if (offset.x < 0f)
            {
                num += gridSize;
            }
            for (float i = num; i < (mGraphRect.x + mGraphRect.width); i += gridSize)
            {
                drawLine(new Vector2(i, mGraphRect.y), new Vector2(i, mGraphRect.y + mGraphRect.height));
            }
            float num3 = mGraphRect.y + offset.y;
            if (offset.y < 0f)
            {
                num3 += gridSize;
            }
            for (float j = num3; j < (mGraphRect.y + mGraphRect.height); j += gridSize)
            {
                drawLine(new Vector2(mGraphRect.x, j), new Vector2(mGraphRect.x + mGraphRect.width, j));
            }
        }

        private void drawLine(Vector2 p1, Vector2 p2)
        {
            GL.Vertex(p1);
            GL.Vertex(p2);
        }

        private Rect mGraphRect;
        private Vector2 mGraphOffset = Vector2.zero;
        private float mGraphZoom = 1f;
        private Material mGridMaterial;
        private float mPrevScreenWidth = -1f;
        private float mPrevScreenHeight = -1f;
        private Vector2 mCurrentMousePosition;
        private bool mSizesInitialized;
        private bool mIsSelecting;
        private bool mIsDragging;
        private Vector2 mDragDelta;
        private bool mNodeClicked;
        private Rect mGraphScrollRect;
        private Rect mFileToolBarRect;
        private Rect mDebugToolBarRect;

        #endregion

        #region Handle Events

        private void handleEvents()
        {
            if (!this.mTakingScreenshot)
            {
                if ((Event.current.type != EventType.MouseUp) && this.CheckForAutoScroll())
                {
                    base.Repaint();
                }
                else if ((Event.current.type != EventType.Repaint) && (Event.current.type != EventType.Layout))
                {
                    switch (Event.current.type)
                    {
                        case EventType.MouseDown:
                            Vector2 vector;
                            if ((Event.current.button != 0) || (Event.current.modifiers == EventModifiers.Control))
                            {
                                if (((Event.current.button == 1) || ((Event.current.modifiers == EventModifiers.Control) && (Event.current.button == 0))) && this.RightMouseDown())
                                {
                                    Event.current.Use();
                                }
                                break;
                            }
                            if (!this.GetMousePositionInGraph(out vector))
                            {
//                                if ((this.GetMousePositionInPropertiesPane(out vector) && (this.mBehaviorToolbarSelection == 2)) && this.mVariableInspector.LeftMouseDown(this.mActiveBehaviorSource, this.mActiveBehaviorSource, vector))
//                                {
//                                    Event.current.Use();
//                                    base.Repaint();
//                                }
                                break;
                            }
                            if (this.LeftMouseDown(Event.current.clickCount, vector))
                            {
                                Event.current.Use();
                            }
                            break;

                        case EventType.MouseUp:
                            if ((Event.current.button != 0) || (Event.current.modifiers == EventModifiers.Control))
                            {
                                if (((Event.current.button == 1) || ((Event.current.modifiers == EventModifiers.Control) && (Event.current.button == 0))) && this.mShowRightClickMenu)
                                {
                                    this.mShowRightClickMenu = false;
                                    this.mRightClickMenu.ShowAsContext();
                                    Event.current.Use();
                                }
                                break;
                            }
                            if (this.LeftMouseRelease())
                            {
                                Event.current.Use();
                            }
                            break;

                        case EventType.MouseMove:
                            if (this.MouseMove())
                            {
                                Event.current.Use();
                            }
                            break;

                        case EventType.MouseDrag:
                            if (Event.current.button != 0)
                            {
                                if ((Event.current.button == 2) && this.MousePan())
                                {
                                    Event.current.Use();
                                }
                                break;
                            }
                            if (!this.LeftMouseDragged())
                            {
                                if ((Event.current.modifiers == EventModifiers.Alt) && this.MousePan())
                                {
                                    Event.current.Use();
                                }
                                break;
                            }
                            Event.current.Use();
                            break;

                        case EventType.KeyDown:
                            if ((Event.current.keyCode == KeyCode.LeftCommand) || (Event.current.keyCode == KeyCode.RightCommand))
                            {
                                this.mCommandDown = true;
                            }
                            break;

                        case EventType.KeyUp:
                            if (((Event.current.keyCode != KeyCode.Delete) && (Event.current.keyCode != KeyCode.Backspace)) && !Event.current.commandName.Equals("Delete"))
                            {
                                if ((Event.current.keyCode == KeyCode.Return) || (Event.current.keyCode == KeyCode.KeypadEnter))
                                {
//                                    if ((this.mBehaviorToolbarSelection == 2) && this.mVariableInspector.HasFocus())
//                                    {
//                                        if (this.mVariableInspector.ClearFocus(true, this.mActiveBehaviorSource))
//                                        {
//                                            this.SaveBehavior();
//                                        }
//                                        base.Repaint();
//                                    }
//                                    else
//                                    {
//                                        this.DisableReferenceTasks();
//                                    }
//                                    Event.current.Use();
                                }
                                else if (Event.current.keyCode == KeyCode.Escape)
                                {
//                                    this.DisableReferenceTasks();
                                }
                                else if ((Event.current.keyCode == KeyCode.LeftCommand) || (Event.current.keyCode == KeyCode.RightCommand))
                                {
                                    this.mCommandDown = false;
                                }
                                break;
                            }
                            if (!this.PropertiesInspectorHasFocus() && !EditorApplication.isPlaying)
                            {
//                                this.DeleteNodes();
                                Event.current.Use();
                                break;
                            }
                            return;

                        case EventType.ScrollWheel:
//                            if (!BehaviorDesignerPreferences.GetBool(BDPreferences.MouseWhellScrolls) || this.mCommandDown)
//                            {
//                                if (this.MouseZoom())
//                                {
//                                    Event.current.Use();
//                                }
//                                break;
//                            }
                            this.MousePan();
                            break;

                        case EventType.ValidateCommand:
                            if (!EditorApplication.isPlaying)
                            {
                                if ((Event.current.commandName.Equals("Copy") || Event.current.commandName.Equals("Paste")) || ((Event.current.commandName.Equals("Cut") || Event.current.commandName.Equals("SelectAll")) || Event.current.commandName.Equals("Duplicate")))
                                {
                                    if ((this.PropertiesInspectorHasFocus() || EditorApplication.isPlaying) || this.ViewOnlyMode())
                                    {
                                        return;
                                    }
                                    Event.current.Use();
                                }
                                break;
                            }
                            return;

                        case EventType.ExecuteCommand:
                            if ((!this.PropertiesInspectorHasFocus() && !EditorApplication.isPlaying) && !this.ViewOnlyMode())
                            {
                                if (Event.current.commandName.Equals("Copy"))
                                {
//                                    this.CopyNodes();
                                    Event.current.Use();
                                }
                                else if (Event.current.commandName.Equals("Paste"))
                                {
//                                    this.PasteNodes();
                                    Event.current.Use();
                                }
                                else if (Event.current.commandName.Equals("Cut"))
                                {
//                                    this.CutNodes();
                                    Event.current.Use();
                                }
                                else if (Event.current.commandName.Equals("SelectAll"))
                                {
//                                    this.mGraphDesigner.SelectAll();
                                    Event.current.Use();
                                }
                                else if (Event.current.commandName.Equals("Duplicate"))
                                {
//                                    this.DuplicateNodes();
                                    Event.current.Use();
                                }
                                break;
                            }
                            return;
                    }
                }
            }
        }

        private bool LeftMouseDown(int clickCount, Vector2 mousePosition)
        {
            if (this.PropertiesInspectorHasFocus())
            {
//                this.mTaskInspector.ClearFocus();
//                this.mVariableInspector.ClearFocus(false, null);
                base.Repaint();
            }
//            NodeDesigner nodeDesigner = this.mGraphDesigner.NodeAt(mousePosition, this.mGraphOffset);
//            if (Event.current.modifiers == EventModifiers.Alt)
//            {
//                this.mNodeClicked = this.mGraphDesigner.IsSelected(nodeDesigner);
//                return false;
//            }
//            if (this.IsReferencingTasks())
//            {
//                if (nodeDesigner == null)
//                {
//                    this.DisableReferenceTasks();
//                }
//                else
//                {
//                    this.ReferenceTask(nodeDesigner);
//                }
//                return true;
//            }
//            if (nodeDesigner != null)
//            {
//                if ((this.mGraphDesigner.HoverNode != null) && !nodeDesigner.Equals(this.mGraphDesigner.HoverNode))
//                {
//                    this.mGraphDesigner.ClearHover();
//                    this.mGraphDesigner.Hover(nodeDesigner);
//                }
//                NodeConnection connection = null;
//                if (!this.ViewOnlyMode() && ((connection = nodeDesigner.NodeConnectionRectContains(mousePosition, this.mGraphOffset)) != null))
//                {
//                    if (this.mGraphDesigner.NodeCanOriginateConnection(nodeDesigner, connection))
//                    {
//                        this.mGraphDesigner.ActiveNodeConnection = connection;
//                    }
//                    return true;
//                }
//                if (nodeDesigner.Contains(mousePosition, this.mGraphOffset, false))
//                {
//                    this.mKeepTasksSelected = false;
//                    if (this.mGraphDesigner.IsSelected(nodeDesigner))
//                    {
//                        if (Event.current.modifiers == EventModifiers.Control)
//                        {
//                            this.mKeepTasksSelected = true;
//                            this.mGraphDesigner.Deselect(nodeDesigner);
//                        }
//                        else if ((Event.current.modifiers == EventModifiers.Shift) && (nodeDesigner.Task is ParentTask))
//                        {
//                            nodeDesigner.Task.get_NodeData().set_Collapsed(!nodeDesigner.Task.get_NodeData().get_Collapsed());
//                            this.mGraphDesigner.DeselectWithParent(nodeDesigner);
//                        }
//                        else if (clickCount == 2)
//                        {
//                            if ((this.mBehaviorToolbarSelection != 3) && BehaviorDesignerPreferences.GetBool(BDPreferences.OpenInspectorOnTaskDoubleClick))
//                            {
//                                this.mBehaviorToolbarSelection = 3;
//                            }
//                            else if (nodeDesigner.Task is BehaviorReference)
//                            {
//                                BehaviorReference task = nodeDesigner.Task as BehaviorReference;
//                                if (((task.GetExternalBehaviors() != null) && (task.GetExternalBehaviors().Length > 0)) && (task.GetExternalBehaviors()[0] != null))
//                                {
//                                    if (this.mLockActiveGameObject)
//                                    {
//                                        this.LoadBehavior(task.GetExternalBehaviors()[0].GetBehaviorSource(), false);
//                                    }
//                                    else
//                                    {
//                                        Selection.activeObject = task.GetExternalBehaviors()[0];
//                                    }
//                                }
//                            }
//                        }
//                    }
//                    else
//                    {
//                        if ((Event.current.modifiers != EventModifiers.Shift) && (Event.current.modifiers != EventModifiers.Control))
//                        {
//                            this.mGraphDesigner.ClearNodeSelection();
//                            this.mGraphDesigner.ClearConnectionSelection();
//                            if (BehaviorDesignerPreferences.GetBool(BDPreferences.OpenInspectorOnTaskSelection))
//                            {
//                                this.mBehaviorToolbarSelection = 3;
//                            }
//                        }
//                        else
//                        {
//                            this.mKeepTasksSelected = true;
//                        }
//                        this.mGraphDesigner.Select(nodeDesigner);
//                    }
//                    this.mNodeClicked = this.mGraphDesigner.IsSelected(nodeDesigner);
//                    return true;
//                }
//            }
//            if (this.mGraphDesigner.HoverNode != null)
//            {
//                bool collapsedButtonClicked = false;
//                if (this.mGraphDesigner.HoverNode.HoverBarButtonClick(mousePosition, this.mGraphOffset, ref collapsedButtonClicked))
//                {
//                    this.SaveBehavior();
//                    if (collapsedButtonClicked && this.mGraphDesigner.HoverNode.Task.get_NodeData().get_Collapsed())
//                    {
//                        this.mGraphDesigner.DeselectWithParent(this.mGraphDesigner.HoverNode);
//                    }
//                    return true;
//                }
//            }
//            List<NodeConnection> nodeConnections = new List<NodeConnection>();
//            this.mGraphDesigner.NodeConnectionsAt(mousePosition, this.mGraphOffset, ref nodeConnections);
//            if (nodeConnections.Count > 0)
//            {
//                if ((Event.current.modifiers != EventModifiers.Shift) && (Event.current.modifiers != EventModifiers.Control))
//                {
//                    this.mGraphDesigner.ClearNodeSelection();
//                    this.mGraphDesigner.ClearConnectionSelection();
//                }
//                for (int i = 0; i < nodeConnections.Count; i++)
//                {
//                    if (this.mGraphDesigner.IsSelected(nodeConnections[i]))
//                    {
//                        if (Event.current.modifiers == EventModifiers.Control)
//                        {
//                            this.mGraphDesigner.Deselect(nodeConnections[i]);
//                        }
//                    }
//                    else
//                    {
//                        this.mGraphDesigner.Select(nodeConnections[i]);
//                    }
//                }
//                return true;
//            }
            if (Event.current.modifiers != EventModifiers.Shift)
            {
//                this.mGraphDesigner.ClearNodeSelection();
//                this.mGraphDesigner.ClearConnectionSelection();
            }
            this.mSelectStartPosition = mousePosition;
            this.mIsSelecting = true;
            this.mIsDragging = false;
            this.mDragDelta = Vector2.zero;
            this.mNodeClicked = false;
            return true;
        }

        private bool RightMouseDown()
        {
            Vector2 vector;
//            if (this.IsReferencingTasks())
//            {
//                this.DisableReferenceTasks();
//                return false;
//            }
            if (!this.GetMousePositionInGraph(out vector))
            {
                return false;
            }
//            NodeDesigner nodeDesigner = this.mGraphDesigner.NodeAt(vector, this.mGraphOffset);
//            if ((nodeDesigner == null) || !this.mGraphDesigner.IsSelected(nodeDesigner))
//            {
//                this.mGraphDesigner.ClearNodeSelection();
//                this.mGraphDesigner.ClearConnectionSelection();
//                if (nodeDesigner != null)
//                {
//                    this.mGraphDesigner.Select(nodeDesigner);
//                }
//            }
//            if (this.mGraphDesigner.HoverNode != null)
//            {
//                this.mGraphDesigner.ClearHover();
//            }
//            this.BuildRightClickMenu(nodeDesigner);
            return true;
        }

        private bool LeftMouseRelease()
        {
            Vector2 vector3;
            this.mNodeClicked = false;
//            if (this.IsReferencingTasks())
//            {
//                Vector2 vector;
//                if (!this.mTaskInspector.IsActiveTaskArray() && !this.mTaskInspector.IsActiveTaskNull())
//                {
//                    this.DisableReferenceTasks();
//                    base.Repaint();
//                }
//                if (!this.GetMousePositionInGraph(out vector))
//                {
//                    this.mGraphDesigner.ActiveNodeConnection = null;
//                    return false;
//                }
//                return true;
//            }
            if (this.mIsSelecting)
            {
                this.mIsSelecting = false;
                return true;
            }
            if (this.mIsDragging)
            {
//                BehaviorUndo.RegisterUndo("Drag", this.mActiveBehaviorSource.get_Owner().GetObject());
//                this.SaveBehavior();
                this.mIsDragging = false;
                this.mDragDelta = Vector3.zero;
                return true;
            }
//            if (this.mGraphDesigner.ActiveNodeConnection != null)
//            {
//                Vector2 vector2;
//                if (!this.GetMousePositionInGraph(out vector2))
//                {
//                    this.mGraphDesigner.ActiveNodeConnection = null;
//                    return false;
//                }
//                NodeDesigner designer = this.mGraphDesigner.NodeAt(vector2, this.mGraphOffset);
//                if (((designer != null) && !designer.Equals(this.mGraphDesigner.ActiveNodeConnection.OriginatingNodeDesigner)) && this.mGraphDesigner.NodeCanAcceptConnection(designer, this.mGraphDesigner.ActiveNodeConnection))
//                {
//                    this.mGraphDesigner.ConnectNodes(this.mActiveBehaviorSource, designer);
//                    BehaviorUndo.RegisterUndo("Task Connection", this.mActiveBehaviorSource.get_Owner().GetObject());
//                    this.SaveBehavior();
//                }
//                else
//                {
//                    this.mGraphDesigner.ActiveNodeConnection = null;
//                }
//                return true;
//            }
//            if ((Event.current.modifiers == EventModifiers.Shift) || this.mKeepTasksSelected)
//            {
//                return false;
//            }
            if (!this.GetMousePositionInGraph(out vector3))
            {
                return false;
            }
//            NodeDesigner nodeDesigner = this.mGraphDesigner.NodeAt(vector3, this.mGraphOffset);
//            if ((nodeDesigner != null) && !this.mGraphDesigner.IsSelected(nodeDesigner))
//            {
//                this.mGraphDesigner.DeselectAll(nodeDesigner);
//            }
            return true;
        }

        private bool MouseMove()
        {
            Vector2 vector;
            if (!this.GetMousePositionInGraph(out vector))
            {
                return false;
            }
//            NodeDesigner designer = this.mGraphDesigner.NodeAt(vector, this.mGraphOffset);
//            if ((this.mGraphDesigner.HoverNode != null) && (((designer != null) && !this.mGraphDesigner.HoverNode.Equals(designer)) || !this.mGraphDesigner.HoverNode.HoverBarAreaContains(vector, this.mGraphOffset)))
//            {
//                this.mGraphDesigner.ClearHover();
//                base.Repaint();
//            }
//            if (((designer != null) && !designer.IsEntryDisplay) && !this.ViewOnlyMode())
//            {
//                this.mGraphDesigner.Hover(designer);
//            }
//            return (this.mGraphDesigner.HoverNode != null);
            return false;
        }

        private bool MousePan()
        {
            Vector2 vector;
            if (!this.GetMousePositionInGraph(out vector))
            {
                return false;
            }
            Vector2 delta = Event.current.delta;
            if (Event.current.type == EventType.ScrollWheel)
            {
                delta = (Vector2) (delta*-1.5f);
                if (Event.current.modifiers == EventModifiers.Control)
                {
                    delta.x = delta.y;
                    delta.y = 0f;
                }
            }
            this.ScrollGraph(delta);
            return true;
        }

        private bool LeftMouseDragged()
        {
            Vector2 vector;
            if (!this.GetMousePositionInGraph(out vector))
            {
                return false;
            }
            if (Event.current.modifiers != EventModifiers.Alt)
            {
//                if (this.IsReferencingTasks())
//                {
//                    return true;
//                }
                if (this.mIsSelecting)
                {
//                    this.mGraphDesigner.DeselectAll(null);
//                    List<NodeDesigner> list = this.mGraphDesigner.NodesAt(this.GetSelectionArea(), this.mGraphOffset);
//                    if (list != null)
//                    {
//                        for (int i = 0; i < list.Count; i++)
//                        {
//                            this.mGraphDesigner.Select(list[i]);
//                        }
//                    }
                    return true;
                }
//                if (this.mGraphDesigner.ActiveNodeConnection != null)
//                {
//                    return true;
//                }
            }
            if (!this.mNodeClicked || this.ViewOnlyMode())
            {
                return false;
            }
            Vector2 zero = Vector2.zero;
//            if (BehaviorDesignerPreferences.GetBool(BDPreferences.SnapToGrid))
//            {
//                this.mDragDelta += Event.current.delta;
//                if (Mathf.Abs(this.mDragDelta.x) > 10f)
//                {
//                    float num2 = Mathf.Abs(this.mDragDelta.x) % 10f;
//                    zero.x = (Mathf.Abs(this.mDragDelta.x) - num2) * Mathf.Sign(this.mDragDelta.x);
//                    this.mDragDelta.x = num2 * Mathf.Sign(this.mDragDelta.x);
//                }
//                if (Mathf.Abs(this.mDragDelta.y) > 10f)
//                {
//                    float num3 = Mathf.Abs(this.mDragDelta.y) % 10f;
//                    zero.y = (Mathf.Abs(this.mDragDelta.y) - num3) * Mathf.Sign(this.mDragDelta.y);
//                    this.mDragDelta.y = num3 * Mathf.Sign(this.mDragDelta.y);
//                }
//            }
//            else
//            {
//                zero = Event.current.delta;
//            }
//            bool flag = this.mGraphDesigner.DragSelectedNodes((Vector2)(zero / this.mGraphZoom), Event.current.modifiers != EventModifiers.Alt);
//            if (flag)
//            {
//                this.mKeepTasksSelected = true;
//            }
//            this.mIsDragging = true;
//            return flag;
            return false;
        }

        #endregion
    }
}